package net.oschina.git.hikoyan.helloapi.authentication;

import net.oschina.git.hikoyan.helloapi.shiro.jwt.authc.UserDefault;
import net.oschina.git.hikoyan.helloapi.shiro.jwt.repository.UserDefaultRepository;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by yan on 17/1/17.
 */
@Repository("userInfoRepository")
public class UserInfoRepository extends UserDefaultRepository {
    private byte[] sharedKey;

    private PasswordService passwordService = new DefaultPasswordService();

    public UserDefault findByUsername(Object username) {
        UserInfo userInfo = new UserInfo("1", "admin", passwordService.encryptPassword("123"));
        Set<String> roles = new HashSet<String>();
        roles.add("admin");
        userInfo.setRoles(roles);
        return userInfo;
    }

    public UserDefault findByUserId(Object userId) {
        return new UserInfo("1", "admin", passwordService.encryptPassword("123"));
    }

    protected String getIssuer() {
        return "issuer";
    }

    protected byte[] getSharedKey() {
        if (sharedKey == null) {
            sharedKey = generateSharedKey();
        }
        return sharedKey;
    }
}
